package com.vipheyue.campusrecyce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.siyamed.shapeimageview.CircularImageView;
import com.vipheyue.campusrecyce.R;
import com.vipheyue.campusrecyce.activity.BrowseActivity;
import com.vipheyue.campusrecyce.activity.LoginActivity;
import com.vipheyue.campusrecyce.bean.MyUser;

import net.soulwolf.widget.speedyselector.widget.SelectorTextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;

public class ReNameFragment3 extends Fragment {

    @Bind(R.id.cirIV_userIcon)
    CircularImageView cirIV_userIcon;
    @Bind(R.id.stv_login)
    SelectorTextView stv_login;
    @Bind(R.id.btn_fragment3_loginout)
    Button btn_fragment3_loginout;
    @Bind(R.id.ll_fragment3_myrelease)
    LinearLayout ll_fragment3_myrelease;
    @Bind(R.id.ll_fragment3_mysale)
    LinearLayout ll_fragment3_mysale;
    @Bind(R.id.ll_fragment3_mybuy)
    LinearLayout ll_fragment3_mybuy;
    @Bind(R.id.rl_menu_left)
    RelativeLayout rl_menu_left;
    @Bind(R.id.tv_Menu_Title)
    TextView tv_Menu_Title;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_re_name_fragment3, container, false);
        ButterKnife.bind(this, view);
        rl_menu_left.setVisibility(View.INVISIBLE);
        tv_Menu_Title.setText("我的");
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        MyUser userInfo = BmobUser.getCurrentUser(getActivity(), MyUser.class);
        if (userInfo != null) {
            stv_login.setText("已登录");
        } else {
//            stv_login.setText("");
        }
//        Toast.makeText(getActivity(), "toast...", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void login() {
        MyUser userInfo = BmobUser.getCurrentUser(getActivity(), MyUser.class);
        if (userInfo == null) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
        } else {
            Toast.makeText(getActivity(), "已登录", Toast.LENGTH_SHORT).show();
        }
        getActivity().finish();
    }

    @OnClick({R.id.cirIV_userIcon, R.id.stv_login, R.id.btn_fragment3_loginout, R.id.ll_fragment3_myrelease, R.id.ll_fragment3_mysale, R.id.ll_fragment3_mybuy})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cirIV_userIcon:
                login();
                break;
            case R.id.stv_login:
                login();
                break;
            case R.id.btn_fragment3_loginout:
                BmobUser.logOut(getActivity());   //清除缓存用户对象
                login();
                break;
            case R.id.ll_fragment3_myrelease:
                Intent intent = new Intent(getActivity(), BrowseActivity.class);
                intent.putExtra("HavedRelease", true);
                startActivity(intent);
                break;
            case R.id.ll_fragment3_mysale:
                break;
            case R.id.ll_fragment3_mybuy:
                Intent intent3 = new Intent(getActivity(), BrowseActivity.class);
                intent3.putExtra("HavedBuy", true);
                startActivity(intent3);
                break;
        }
    }
}
